#include "imagecomparator.h"
#include "colorhistogram.h"

using namespace controllers;

/**
 * @brief ImageComparator::ImageComparator
 */
ImageComparator::ImageComparator() : _div(32) {
}

/**
 * @brief ImageComparator::~ImageComparator
 */
ImageComparator::~ImageComparator()
{
}

/**
 * @brief ImageComparator::setColorReduction
 *        Color reduction factor
 *        The comparison will be made on images with
 *        color space reduced by this factor in each dimension
 * @param factor
 */
void ImageComparator::setColorReduction(int factor)
{
    _div = factor;
}

/**
 * @brief ImageComparator::getColorReduction
 * @return
 */
int ImageComparator::getColorReduction()
{
    return _div;
}

/**
 * @brief ImageComparator::setReferenceImage
 *        Set the image reference to compare with
 * @param image
 */
void ImageComparator::setReferenceImage(const Mat& image)
{
    ColorHistogram cHist;

    //reference   = hist.colorReduce(image, div);
    _reference      = image;
    _refHist        = cHist.getHistogram(_reference);
    _refHueHist     = cHist.getHueHistogram(_reference);
    _refSparseHist  = cHist.getSparseHistogram(_reference);
}

/**
 * @brief ImageComparator::compare
 *        Compares an image with the reference image
 * @param image
 * @return
 */
double ImageComparator::compareWithReference(const Mat& image, ComparisonTypes type)
{
    ColorHistogram cHist;

    //input  = cHist.colorReduce(image, div);
    _input  = image;

    if (type == NORMAL)
    {
        _inputH = cHist.getHistogram(_input);
        return compareHist(_refHist, _inputH, CV_COMP_INTERSECT);
    }
    else if (type == HUE)
    {
        _inputH = cHist.getHueHistogram(_input);
        return compareHist(_refHueHist, _inputH, CV_COMP_INTERSECT);
    }
    else if (type == SPARSE)
    {
        _inputH = cHist.getSparseHistogram(_input);
        return compareHist(_refSparseHist, _inputH, CV_COMP_INTERSECT);
    }
}

double ImageComparator::compareWithRefWithRef(ComparisonTypes type)
{
    if (type == NORMAL)
    {
        return compareHist(_refHist, _refHist, CV_COMP_INTERSECT);
    }
    else if (type == HUE)
    {
        return compareHist(_refHueHist, _refHueHist, CV_COMP_INTERSECT);
    }
    else if (type == SPARSE)
    {
        return compareHist(_refSparseHist, _refSparseHist, CV_COMP_INTERSECT);
    }
}

/**
 * @brief ImageComparator::compareHistogram
 *        Compares two image histograms
 * @param hist1
 * @param hist2
 * @return
 */
double ImageComparator::compareHistograms(const SparseMat& hist1, const SparseMat& hist2, ComparisonTypes type)
{
    if (type == NORMAL)
    {
        return compareHist(hist1, hist2, CV_COMP_INTERSECT);
    }
    else if (type == HUE)
    {
        return compareHist(hist1, hist2, CV_COMP_INTERSECT);
    }
    else if (type == SPARSE)
    {
        return compareHist(hist1, hist2, CV_COMP_INTERSECT);
    }
}
